function onLoad(){
	document.title=window.arguments[0];
	buildFarms();
	// Size of view
	var size=wind().get_var("farmtsize");
	var tree=document.getElementById("farms");
	tree.setAttribute("rows",size);
	
	if(typeof window.arguments[2]!="undefined"){
		document.getElementById("c4").setAttribute("src","chrome://twbooster/skin/default/submit.png");
		document.getElementById("c4").setAttribute("label",wind().lang("send"));
	}
}

function wind(){
	return window.arguments[1].TWB;
}

function clearAll(){
	var i;
	var list;
	var tree=document.getElementById("farms");
	list=tree.getElementsByTagName("treeitem");
	for(i=list.length-1; i>=0; i++){
		list[i].parentNode.removeChild(list[i]);
	}
}

function buildFarms(){
	clearAll();
	wind().Modules.FarmTool.FARMS=wind().get_var("farms_"+wind().language+"_"+wind().World(),{});
	var i;
	var j;
	var to;
	var src;
	var vil;
	var from;
	var distance;
	var tree=document.getElementById("farms");
	for(vil in wind().Modules.FarmTool.FARMS){
		if(!wind().gei("TWB-v"+vil)){
			// Possible ways that villages don't exist anymore
			continue;
		}
		var item=document.createElement("treeitem");
		item.setAttribute("container",true);
		item.setAttribute("open",true);
		var row=document.createElement("treerow");
		var cell=document.createElement("treecell");
		cell.setAttribute("label",wind().gei("TWB-v"+vil).getAttribute("value"));
		cell.setAttribute("editable",false);
		from=wind().gei("TWB-v"+vil).getAttribute("value").match(/\d+\|\d+/)[0].split("|");
		row.appendChild(cell);
		item.appendChild(row);
		var childr=document.createElement("treechildren");
		childr.setAttribute("id",vil);
		for(i=0; i<wind().Modules.FarmTool.FARMS[vil].length; i++){
			var obj=wind().Modules.FarmTool.FARMS[vil][i];
			to=obj.coords.split("|");
			distance=Math.round(100*Math.sqrt(Math.pow(from[0]-to[0],2)+Math.pow(from[1]-to[1],2)))/100;
			var item_=document.createElement("treeitem");
			var row_=document.createElement("treerow");
			row_.setAttribute("id","farm_"+obj.id);
			var cell1=document.createElement("treecell");
			cell1.setAttribute("label",obj.coords);
			cell1.setAttribute("editable",false);
			var cell2=document.createElement("treecell");
			cell2.setAttribute("label",distance);
			cell2.setAttribute("editable",false);
			var cell3=document.createElement("treecell");
			cell3.setAttribute("label",obj.units);
			cell3.setAttribute("editable",false);
			var cell4=document.createElement("treecell");
			cell4.setAttribute("label",obj.label);
			cell4.setAttribute("editable",typeof window.arguments[2]=="undefined");
			var cell5=document.createElement("treecell");
			cell5.setAttribute("editable",true);
			cell5.setAttribute("value",false);
			cell5.setAttribute("class","del");
			row_.appendChild(cell1);
			row_.appendChild(cell2);
			row_.appendChild(cell3);
			row_.appendChild(cell4);
			row_.appendChild(cell5);
			item_.appendChild(row_);
			childr.appendChild(item_);
		}
		item.appendChild(childr);
		tree.appendChild(item);
	}
}

function onOK(){
	try{
		var id;
		var farm;
		var farms;
		if(typeof window.arguments[2]!="undefined"){
			// Return checked in a linearized array
			var send=[];
			for(id in wind().Modules.FarmTool.FARMS){
				farms=document.getElementById(id);
				for(i=0; i<wind().Modules.FarmTool.FARMS[id].length; i++){
					farm=document.getElementById("farm_"+wind().Modules.FarmTool.FARMS[id][i].id);
					if(farm!=null && farm.childNodes[4].getAttribute("value")=="true"){
						send.push([id,wind().Modules.FarmTool.FARMS[id][i].id,wind().Modules.FarmTool.FARMS[id][i].units]);
					}
				}
			}
			window.arguments[2].out=send;
			return;
		}
		
		// Confirm Delete
		var i;
		var j;
		var tmp;
		var todelete=[];
		var flag=false;
		var cells=document.getElementsByClassName("del");
		for(i=0; i<cells.length; i++){
			if(cells[i].getAttribute("value")=="true"){
				flag=true;
				break;
			}
		}
		// Save the whole thing
		if(flag==false || (flag==true && confirm(wind().lang("confdelfarms")))){
			// First Update
			for(id in wind().Modules.FarmTool.FARMS){
				farms=document.getElementById(id);
				for(i=0; i<wind().Modules.FarmTool.FARMS[id].length; i++){
					farm=document.getElementById("farm_"+wind().Modules.FarmTool.FARMS[id][i].id);
					if(farm.childNodes[4].getAttribute("value")=="true"){
						// Schedule for delete 
						todelete.push([id,wind().Modules.FarmTool.FARMS[id][i].id]);
					}
					else{				
						wind().Modules.FarmTool.FARMS[id][i].units=farm.childNodes[2].getAttribute("label");
						wind().Modules.FarmTool.FARMS[id][i].label=farm.childNodes[3].getAttribute("label");
					}
				}
			}
			// Then Delete
			for(i=0; i<todelete.length; i++){
				var tmp=wind().Modules.FarmTool.FARMS[todelete[i][0]];
				for(j=0; j<tmp.length; j++){
					if(tmp[j].id==todelete[i][1]){
						wind().Modules.FarmTool.FARMS[todelete[i][0]].splice(j,1);
						break;
					}
				}
				// If no farms for a vil delete the whole object for that id
				if(wind().Modules.FarmTool.FARMS[todelete[i][0]].length==0){
					delete wind().Modules.FarmTool.FARMS[todelete[i][0]];
				}
			}
			// Save
			wind().set_var("farms_"+wind().language+"_"+wind().World(),wind().Modules.FarmTool.FARMS);
			return true;
		}
		else{
			return false;
		}
	}catch(e){
		alert("[#F] Error : "+e.message);
	}
}

function onSubmit(units){
	var tree = document.getElementById("tree");
	tree.view.setCellText(window.arguments[2], tree.columns.getColumnAt(2), units);
}

function trackClick(){
	if(typeof window.arguments[2]!="undefined"){
		return;
	}
	
	var i;
	var tree = document.getElementById("tree");
	var selection = tree.view.selection;
	window.arguments[2]=tree.currentIndex;
	try{
		var units = tree.view.getCellText(tree.currentIndex, tree.columns.getColumnAt(2));
	}catch(e){
		return;
	}
	// Transform into images
	units=units.match(/\d+[a-z]/g);
	wind().Modules.Units.Cancel(window);
	wind().Modules.Units.Show(window);
	if(units){
		for(i=0; i<units.length; i++){
			switch(units[i].match(/[a-z]/)[0]){
				case "p": wind().Modules.Units.Spear(window,units[i].match(/\d+/)[0]); break;
				case "w": wind().Modules.Units.Sword(window,units[i].match(/\d+/)[0]); break;
				case "a": wind().Modules.Units.Axe(window,units[i].match(/\d+/)[0]); break;
				case "e": wind().Modules.Units.Archer(window,units[i].match(/\d+/)[0]); break;
				case "s": wind().Modules.Units.Spy(window,units[i].match(/\d+/)[0]); break;
				case "l": wind().Modules.Units.Light(window,units[i].match(/\d+/)[0]); break;
				case "m": wind().Modules.Units.Marcher(window,units[i].match(/\d+/)[0]); break;
				case "h": wind().Modules.Units.Heavy(window,units[i].match(/\d+/)[0]); break;
				case "r": wind().Modules.Units.Ram(window,units[i].match(/\d+/)[0]); break;
				case "c": wind().Modules.Units.Catapult(window,units[i].match(/\d+/)[0]); break;
				case "k": wind().Modules.Units.Knight(window,units[i].match(/\d+/)[0]); break;
				case "b": wind().Modules.Units.Snob(window,units[i].match(/\d+/)[0]); break;
			}
		}
	}
}

function checkAll(){
	var id;
	var farm;
	var farms;
	for(id in wind().Modules.FarmTool.FARMS){
		farms=document.getElementById(id);
		for(i=0; i<wind().Modules.FarmTool.FARMS[id].length; i++){
			farm=document.getElementById("farm_"+wind().Modules.FarmTool.FARMS[id][i].id);
			if(farm!=null){
				farm.childNodes[4].setAttribute("value","true");
			}
		}
	}
}

function checkNone(){
	var id;
	var farm;
	var farms;
	for(id in wind().Modules.FarmTool.FARMS){
		farms=document.getElementById(id);
		for(i=0; i<wind().Modules.FarmTool.FARMS[id].length; i++){
			farm=document.getElementById("farm_"+wind().Modules.FarmTool.FARMS[id][i].id);
			if(farm!=null){
				farm.childNodes[4].setAttribute("value","false");
			}
		}
	}
}